import http from '../../apis/api.js';
import util from '../../utils/util.js';
let self;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    /**分页数据 */
    jzUser: false,
    jyUser: false,
    pagination: {
      currentPage: 1,
      pageSize: 6,
      more: 0
    },
    keywords: '',
    loading: false,
    materialList: [],
    isSign: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this;
    self.data.isSign = Number(options.isSign),
      self.setData({
        isSign: self.data.isSign
      })
    util.getRole().then(res => {
      self.data.jzUser = res.indexOf('jz_user') > -1;
      self.data.jyUser = res.indexOf('jy_user') > -1;
      self.setData({
        jzUser: self.data.jzUser,
        jyUser: self.data.jyUser
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    self.data.keywords = "";
    self.data.pagination.currentPage = 1;
    self.data.materialList = [];
    self.getMaterialList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (self.data.pagination.more > 0) {
      self.data.pagination.currentPage++;
      self.getMaterialList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**获取备件列表*/
  getMaterialList() {
    let postData = {
      currentPage: self.data.pagination.currentPage,
      pageSize: self.data.pagination.pageSize,
      isSign: self.data.isSign
    }
    self.setData({
      'loading': true
    })
    http.partsList(postData).then(res => {
      self.data.materialList = self.data.materialList.concat(res.content.records);
      self.data.pagination.more = Number(res.content.current) * Number(res.content.size) < res.content.total ? 1 : 0;
      self.setData({
        'loading': false,
        'materialList': self.data.materialList,
        'pagination': self.data.pagination
      })
    })
  },
  changeState(e) {
    self.data.isSign = e.currentTarget.dataset.state ? Number(e.currentTarget.dataset.state) : null;
    self.data.pagination.currentPage = 1;
    self.data.materialList = [];
    self.setData({
      isSign: self.data.isSign
    })
    self.getMaterialList();
  },
  goRouter(e) {
    console.log(e);
    let type = e.currentTarget.dataset.type;
    let url
    switch (type) {
      case 'add':
        url = `/pages/addParts/addParts?type=add`
        break;
    }
    wx.navigateTo({
      url,
    })
  },
  optionHandle(e) {
    console.log(e);
    let dataset = e.currentTarget.dataset;
    switch (dataset.type) {
      case 'sign':
        wx.showModal({
          title: '提示',
          content: '是否确认签收',
          success(res) {
            if (res.confirm) {
              http.updateParts({
                id: dataset.item.id,
                isSign:1
              }).then(res=>{
                wx.showToast({
                  title: '签收成功',
                  icon: 'none',
                  duration: 2000
                })
                self.data.pagination.currentPage = 1;
                self.data.materialList = [];
                self.getMaterialList();
              })
            }
          }
        })
        break;
      case 'detail':
        wx.navigateTo({
          url: `/pages/partsDetail/partsDetail?id=${dataset.id}`,
        })
        break;
    }
  }
})